import requests
import json




# 登录接口
host = 'http://shop-xo.hctestedu.com'

pub_pararms = {
    "application": "app",
    "application_client_type": "pc",
    "s": "api/user/login"
}

data_body = {
    "accounts": "felix_test",
    "pwd": "11081103",
    "type": "username"
}

header = {
    "Content-Type": "Application/json"
}
data_body_link = 'accounts=felix_test&pwd=11081103&type=username'

re = requests.request("POST",url=host,headers=header,params=pub_pararms,data=json.dumps(data_body))

# res = re.json()
# print(re)
# print(re.url)
# print(re.request.url)
# print("请求头:",re.request.headers)
# print("响应头:",re.headers)
# print(re.request.body)
# print(res["msg"])


#TODO 获取响应数据msg
import jsonpath

msg = jsonpath.jsonpath(re.json(),"$..msg")[0]
token = jsonpath.jsonpath(re.json(),"$..token")[0]

print("获取到的数据:",msg)
print("获取到的token:",token)


# TODO 通过对应的msg进行断言处理
assert "登录成功" == msg, f"不成功显示：期望结果:登录成功，实际结果:{msg}"


# 加入购物车
pub_pararms_car = {
    "application": "app",
    "application_client_type": "pc",
    "s": "api/cart/save",
    "token": token
}

data = {
    "goods_id": "11",
    "spec": [
        {
            "type": "尺寸",
            "value": "M"
        }
    ],
    "stock": "10"
}

re_car = requests.post(url=host,headers=header,params=pub_pararms_car,json=data)

print(f"请求的url:{re_car.request.url}")
print(f"响应数据:{re_car.text}")

msg_car = jsonpath.jsonpath(re_car.json(),"$..msg")[0]

print(msg_car)

assert "加入成功" == msg_car, f"不成功提示，预期结果:加入成功，实际结果:{msg_car}"


#获取购物车列表
pub_pararms_car_get = {
    "application": "app",
    "application_client_type": "pc",
    "s": "api/cart/index",
    "token": token
}


get_car_list = requests.get(url=host,params=pub_pararms_car_get,headers=header)

cart_id = jsonpath.jsonpath(get_car_list.json(),"$..id")[0]
print(get_car_list.json())

print(f"获取购物车商品id:{cart_id}")


# 获取地址列表
pub_pararms_addressList_get = {
    "application": "app",
    "application_client_type": "pc",
    "s": "api/useraddress/index",
    "token": token
}

get_address_list = requests.post(url=host,params=pub_pararms_addressList_get,headers=header)

address_id = jsonpath.jsonpath(get_address_list.json(),"$..id")[0]
print(get_address_list.json())
print(f"获取地址id:{address_id}")



# 购物车提交订单
pub_pararms_car_buyAdd = {
    "application": "app",
    "application_client_type": "pc",
    "s": "api/buy/add",
    "token": token
}

buy_add_data = {
    "buy_type": "cart",
    "ids": cart_id,
    "address_id": address_id,
    "payment_id": 4,
    "site_model": 0
}

buy_add_cart_res = requests.post(url=host,headers=header,params=pub_pararms_car_buyAdd,  json=buy_add_data)

print(buy_add_cart_res.request.url)
print(buy_add_cart_res.request.body)
print(buy_add_cart_res.json())


# host = 'https://echo.apifox.com'
# path = '/anything'
# url = host + path
# pub_params = {
#     "type": "App",
#     "client": "Pc"
# }
#
# data_body = 'name=felix&passwd=123456'
# headers = {
#     "Content-Type": "application/json",
# }
#
# data_body_dic = {
#     "name": "felix樊子丰",
#     "passwd": "123456"
# }
#
# re = requests.post(url=url,params=pub_params,data=json.dumps(data_body_dic),headers=headers)
#
#
#
# re.encoding = 'utf-8'
# # print("响应状态码:",re.status_code)
# print("请求url：", re.request.url)
# print("请求头:", re.request.headers)
# print("响应头:", re.headers)
# print("请求体:",re.request.body.encode('utf-8').decode('unicode_escape'))
# rejson = re.json()
# reText = re.text
# reContent = re.content
# print("返回json:",rejson)
# print("返回json,通过key取值:",rejson["data"].encode('utf-8').decode('unicode_escape'))
# print("返回Text:",reText)
# reTextJson = json.loads(reText)
# print("返回reTextJson:",reTextJson)
#
# def convert_unicode(obj):
#     if isinstance(obj, dict):
#         return {key: convert_unicode(value) for key, value in obj.items()}
#     elif isinstance(obj, list):
#         return [convert_unicode(item) for item in obj]
#     elif isinstance(obj, str):
#         if "\\u" in obj:
#             # 如果字符串中包含 Unicode 转义序列，手动处理
#             return obj.encode('utf-8').decode('unicode_escape')
#         else:
#             return obj
#     else:
#         return obj
# reTextJson = convert_unicode(reTextJson)
# print("返回Text--JSON:",json.dumps(reTextJson,sort_keys=True, indent=4, separators=(',', ':'),ensure_ascii=False))
#
# print("返回reContent:",reContent.decode("utf-8").\
#       replace(r'\\u', r'\u').\
#       encode('utf-8').decode('unicode_escape'))
# print("返回reContent:",reContent)
# print("返回reContent:",json.loads(reContent))



# url = "https://www.baidu.com/img/flexible/logo/pc/result.png"
#
# re = requests.request("GET",url=url)
#
# print(re.content)
#
# with open('result11.png','wb') as f:
#     f.write(re.content)
#
# #

